package jee.boot.module.quartz.service;

import jee.boot.common.basic.CrudService;
import jee.boot.common.basic.ServiceException;
import jee.boot.module.quartz.dao.QuartzDao;
import jee.boot.module.quartz.entity.Quartz;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 任务调度Service
 *
 * @author zhilei.wang
 * @version 2018-01-25
 */
@Service
public class QuartzService extends CrudService<QuartzDao, Quartz> {
    @Autowired
    private Scheduler scheduler;

    /**
     * 新增定时任务
     *
     * @param entity
     */
    @Override
    public void save(Quartz entity) {
        try {
            Class cls = Class.forName(entity.getClassName());
            cls.newInstance();
            //构建job信息
            JobDetail job = JobBuilder.newJob(cls).withIdentity(entity.getName(), entity.getGroup()).withDescription(entity.getDescription()).build();
            // 触发时间点
            CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(entity.getCronExpression());
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger" + entity.getName(), entity.getGroup()).startNow().withSchedule(cronScheduleBuilder).build();
            //交由Scheduler安排触发
            scheduler.scheduleJob(job, trigger);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceException(1413);
        }
    }

    /**
     * 修改定时任务
     *
     * @param entity
     */
    @Override
    public void update(Quartz entity) {
        try {
            // 删除原来的
            this.delete(entity);
            //重新保存新的
            this.save(entity);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceException(1414);
        }
    }

    /**
     * 删除定时任务
     *
     * @param entity
     */
    @Override
    public void delete(Quartz entity) {
        entity = get(entity);
        try {
            JobKey jobkey = JobKey.jobKey(entity.getName(), entity.getGroup());
            TriggerKey triggerKey = TriggerKey.triggerKey(entity.getName(), entity.getGroup());
            // 停止触发器
            scheduler.pauseTrigger(triggerKey);
            // 移除触发器
            scheduler.unscheduleJob(triggerKey);
            scheduler.deleteJob(jobkey);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceException(1415);
        }
    }

    /**
     * 触发定时任务
     *
     * @param entity
     */
    public void trigger(Quartz entity) {
        entity = get(entity);
        try {
            JobKey jobkey = JobKey.jobKey(entity.getName(), entity.getGroup());
            scheduler.triggerJob(jobkey);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw new ServiceException(1416);
        }
    }

    /**
     * 暂停定时任务
     *
     * @param entity
     */
    public void pause(Quartz entity) {
        entity = get(entity);
        try {
            JobKey jobkey = JobKey.jobKey(entity.getName(), entity.getGroup());
            scheduler.pauseJob(jobkey);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw new ServiceException(1417);
        }
    }

    /**
     * 恢复定时任务
     *
     * @param entity
     */
    public void resume(Quartz entity) {
        entity = get(entity);
        try {
            JobKey jobkey = JobKey.jobKey(entity.getName(), entity.getGroup());
            scheduler.resumeJob(jobkey);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            throw new ServiceException(1418);
        }
    }
}
